package ar.edu.unicen.pladema.ui.mywidgets;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.ColorDialog;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

import ar.edu.unicen.pladema.main.SWTUtils;
import ar.edu.unicen.pladema.ui.examples.NumberInputDialog;
import ar.edu.unicen.pladema.ui.panel.AbstractPanel;

public class ColorListItem extends AbstractPanel {
	private Label altura;
	private Label colorL;
	//private Composite color;
	private ColorList parent;

	public ColorListItem(ColorList parent) {
		super(parent);
		this.parent = parent;
	}
	public ColorListItem(Composite parent, ColorList colorList) {
		super(parent);
		this.parent = colorList;
	}

	@Override
	protected void createWidgets() {
		altura = new Label(this, SWT.NONE);
		//color = new Composite(this, SWT.NONE );
		colorL = new Label(this, SWT.NONE);
		altura.setText("toma");
		colorL.setText("    ");
		colorL.addMouseListener(new MouseAdapter() {
			
			@Override
			public void mouseUp(MouseEvent arg0) {
				ColorDialog cd = new ColorDialog(getShell());
		          cd.setText("Seleccione el color");
		          cd.setRGB(colorL.getBackground().getRGB());
		          RGB newColor = cd.open();
		          if (newColor == null) {
		            return;
		          }
		          colorL.setBackground(new Color(getDisplay(), newColor));
			}
		});
		altura.addMouseListener(new MouseAdapter() {
			
			@Override
			public void mouseUp(MouseEvent arg0) {
				if (parent == null) {
					System.out.println("parent null");
					return;
				}
				NumberInputDialog nid = new NumberInputDialog(getShell());
				nid.setText("Seleccione el color");
		        Integer newAltura = nid.open();
		        if (newAltura == null) {
		            return;
		        }
		        altura.setText(newAltura.toString());
		        parent.sort();
			}

		});
	}


	@Override
	protected void layoutWidgets() {
		this.setLayout(new FormLayout());
		colorL.setLayoutData(SWTUtils.createRightFormData(0));
		altura.setLayoutData(SWTUtils.createFullLeftFormData(colorL));
	}
	public void setText(String s) {
		altura.setText(s);
	}
	public String getText() {
		return altura.getText();
	}
	public void setColor(RGB c) {
		colorL.setBackground(new Color(this.getDisplay(), c));
	}
	public RGB getColor() {
		return colorL.getBackground().getRGB();
	}
}
